package com.uphone.kingmall.adapter;

import android.app.Activity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.blankj.utilcode.util.SizeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.china.zhuoyue.R;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.uphone.kingmall.activity.ShoppingCartActivity;
import com.uphone.kingmall.bean.GoodsDetailBean;
import com.uphone.kingmall.bean.ShoppingCartBean;
import com.uphone.kingmall.dialog.ShopCartPw;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.main.MainActivity;
import com.uphone.kingmall.utils.GlideUtils;
import com.uphone.kingmall.utils.GsonUtils;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.utils.ToastUtils;
import com.uphone.kingmall.view.AmountView;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *     author : radish
 *     e-mail : 15703379121@163.com
 *     time   : 2019/3/20
 *     desc   :
 * </pre>
 */
public class ShoppingCartItemAdapter extends BaseQuickAdapter<ShoppingCartBean.ShopCarBean.GoodsBean, BaseViewHolder> {


    private final Activity activity;
    private OnGoodsItemClickListener listener;

    public ShoppingCartItemAdapter(Activity activity) {
        super(R.layout.kingmall_item_item_shopping_cart);
        this.activity = activity;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final ShoppingCartBean.ShopCarBean.GoodsBean item) {
        final CheckBox ivSelect = helper.getView(R.id.iv_select);
        ivSelect.setChecked(item.isSelect());
        ImageView iv_icon = helper.getView(R.id.iv_icon);
        GlideUtils.getInstance().loadCornerImage(mContext, item.getGoodsMainImg(), SizeUtils.dp2px(2), iv_icon);
        helper.setText(R.id.tv_name, item.getGoodsName() + "");
        helper.setText(R.id.tv_goods_spec, item.getGoodsSpec() + "");
        helper.setText(R.id.tv_price, "¥" + item.getGoodsPerFee());

        ivSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                item.setSelect(isChecked);
                if (listener != null) {
                    listener.itemSelect(ivSelect, helper.getLayoutPosition(), item);
                }
            }
        });

        helper.getView(R.id.ll).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.itemClick(v, helper.getLayoutPosition(), item);
                }
            }
        });

        helper.getView(R.id.tv_goods_spec).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int goodsId = item.getGoodsId();

                HttpParams params = new HttpParams();
                params.put("goodsId", goodsId);

                //获取商品详情
                OkGoUtils.progressRequest(MyUrl.getGoodsInfo, mContext, params, new onNormalRequestListener() {
                    @Override
                    public void onSuccess(String body, int code) {
                        GoodsDetailBean bean = GsonUtils.getGson().fromJson(body, GoodsDetailBean.class);
                        if (bean != null) {
                            openCartList(helper, item, bean);
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {

                    }
                });
            }
        });

        AmountView amoutView = helper.getView(R.id.amount_view);
        amoutView.setGoodsAmount(item.getGoodsNum());
        amoutView.setGoods_quota(item.getRepertoryTim());
        amoutView.setGoods_storage(item.getRepertoryNum());

        amoutView.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
            @Override
            public void onAmountChange(View view, int amount, int type) {
//                item.setGoodsNum(amount);
                if (listener != null) {
                    listener.itemAmount(view, helper.getLayoutPosition(), item, amount, type);
                }
            }
        });

    }

    private void openCartList(final BaseViewHolder helper, final ShoppingCartBean.ShopCarBean.GoodsBean item, final GoodsDetailBean bean) {//1 加入购物车 2规格
        if (bean == null || bean.getGoods() == null) {
            ToastUtils.showShortToast(mContext, "未找到商品");
            return;
        }
        if (bean.getGoods().getGoodsType() != 3 && bean.getGoods().getGoodsType() != 4 && bean.getGoods().getRepertoryNum() <= 0) {
            ToastUtils.showShortToast(mContext, "库存不足");
            return;
        }
        if (bean.getGoods().getProps() == null || bean.getGoods().getProps().size() == 0) {
            return;
        }
        List<GoodsDetailBean.GoodsBean.PropsBean.PropBean> goodPropList = new ArrayList<>();
        String goodsPropIds = item.getGoodsPropId();
        if (!TextUtils.isEmpty(goodsPropIds)) {
            String[] split = goodsPropIds.split(",");
            if (split != null && split.length > 0) {
                GoodsDetailBean.GoodsBean.PropsBean.PropBean goodPropBean = null;
                for (int i = 0; i < bean.getGoods().getProps().size(); i++) {
                    for (int j = 0; j < bean.getGoods().getProps().get(i).getProp().size(); j++) {
                        for (int k = 0; k < split.length; k++) {
                            if (TextUtils.equals(bean.getGoods().getProps().get(i).getProp().get(j).getGoodsPropId() + "", split[k])) {
                                goodPropBean = bean.getGoods().getProps().get(i).getProp().get(j);
                                goodPropList.add(goodPropBean);
                            }
                        }
                    }
                }
            }
        }
        ShopCartPw shopCartPw = new ShopCartPw(mContext, false, bean.getGoods(), goodPropList, new ShopCartPw.setOnDialogClickListener() {

            @Override
            public void onClick(View v, int type, String pos, String num, String price) {
                switch (v.getId()) {
                    case R.id.pw_cart_btn://确定
                        break;
                }
            }

            @Override
            public void onClick(View v, int type, List<GoodsDetailBean.GoodsBean.PropsBean> propsList, List<GoodsDetailBean.GoodsBean.PropsBean.PropBean> propList, String num, String price) {
                switch (v.getId()) {
                    case R.id.pw_cart_btn://确定
                        if (type == 666) {
                            // 确定规格
                            HttpParams params = new HttpParams();
                            params.put("carId", item.getShopCarId());
                            String propIds = "";
                            String propStrs = "";
                            for (int i = 0; i < propList.size(); i++) {
                                propIds += propList.get(i).getGoodsPropId() + ",";
                                propStrs += propList.get(i).getGoodsPropVal() + " ";
                            }
                            if (propIds.length() > 1) {
                                params.put("goodsPropIds", propIds.substring(0, propIds.length() - 1));
                            }


                            final String finalPropStrs = propStrs;
                            OkGoUtils.progressRequest(MyUrl.updateProp, mContext, params, new onNormalRequestListener() {
                                @Override
                                public void onSuccess(String body, int code) {
                                    if (code == 0) {
                                        helper.setText(R.id.tv_goods_spec, finalPropStrs + "");
                                        if (activity instanceof ShoppingCartActivity) {
                                            ((ShoppingCartActivity) activity).loadData();
                                        } else if (activity instanceof MainActivity) {
                                            ((MainActivity) activity).reloadShoppingCart();
                                        }
                                    }
                                }

                                @Override
                                public void onError(Response<String> response) {

                                }
                            });
                        }
                        break;
                }
            }
        }, new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
            }

        });
        shopCartPw.showAtLocation(shopCartPw.popupView.getRootView(), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    public void setOnGoodsItemClickListener(OnGoodsItemClickListener listener) {
        this.listener = listener;
    }

    interface OnGoodsItemClickListener {
        void itemClick(View v, int position, ShoppingCartBean.ShopCarBean.GoodsBean item);

        void itemSelect(View v, int position, ShoppingCartBean.ShopCarBean.GoodsBean item);

        void itemAmount(View v, int position, ShoppingCartBean.ShopCarBean.GoodsBean item, int amount, int type);
    }
}
